package cn.tworice.client.web;

import cn.tworice.admin.service.PostsService;
import cn.tworice.admin.service.StudentService;
import cn.tworice.client.service.InfoEditService;
import cn.tworice.common.entity.Companies;
import cn.tworice.common.entity.ResumeReceived;
import cn.tworice.common.entity.ShowPostsLog;
import cn.tworice.common.entity.Student;
import cn.tworice.common.entity.dto.PostsDto;
import cn.tworice.common.entity.dto.ResumeReceivedDto;
import com.lmk.core.web.support.api.RequestResult;
import com.lmk.core.web.support.api.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("client/infoEdit")
@CrossOrigin
public class InfoEditClient {

    @Autowired
    private InfoEditService infoEditService;

    @Autowired
    private PostsService postsService;

    @Autowired
    private StudentService studentService;


    @PutMapping("companies")
    public RequestResult companies(Companies companies) {
        RequestResult result = new RequestResult();
        infoEditService.setCompanies(companies);
        result.appendData("info", infoEditService.getCompaniesById(companies.getId()));
        return result;
    }

    @GetMapping("degree")
    @ApiOperation("获取学历列表")
    public RequestResult degree() {
        RequestResult result = new RequestResult();
        result.appendData("degree", postsService.getDegree());
        return result;
    }

    @PutMapping("student")
    public RequestResult student(Student student) {
        RequestResult result = new RequestResult();
        studentService.setStudent(student);
        result.appendData("info", studentService.getStudentById(student.getId()));
        return result;
    }

    @GetMapping("posts")
    public RequestResult posts(PostsDto postsDto) {
        RequestResult result = new RequestResult();
        List<PostsDto> list = postsService.getList(postsDto, 0, 1000);
        result.appendData("list", list);
        return result;
    }

    @PutMapping("showPosts")
    public RequestResult showPosts(ShowPostsLog showPostsLog) {
        RequestResult result = new RequestResult();
        infoEditService.setShowPostsLog(showPostsLog);
        return result;
    }

    @GetMapping("showPosts")
    public RequestResult showPosts(String studentId){
        RequestResult result = new RequestResult();
        List<PostsDto> list=infoEditService.getShowPostsLog(studentId);
        result.appendData("list", list);
        return result;

    }

    /**
     * 上传PDF 返回PDF文件名
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 14:53
     */
    @PostMapping("upload")
    public RequestResult getPDF(MultipartFile file,String studentId){
        RequestResult result = new RequestResult();
        String name=infoEditService.saveFile(file);
        infoEditService.setResumes(studentId, name);
        result.appendData("file", name);
        return result;
    }

    /**
     * 通过学生ID获取简历
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 22:03
     */
    @GetMapping("resumes")
    public RequestResult resumes(String studentId){
        RequestResult result = new RequestResult();
        String file=infoEditService.getResumesByStudent(studentId);
        result.appendData("file", file);
        return result;
    }

    /**
     * 投递简历
     * @Author 二饭
     * @Email 1786705138@qq.com
     * @Date 2021/12/15 16:39
     */
    @PostMapping("resumes")
    public RequestResult resumes(ResumeReceived resumeReceived) {
        RequestResult result = new RequestResult();
        String message=infoEditService.setResumeReceived(resumeReceived);
        result.setCode(new StatusCode(200,message));
        return result;
    }

    @GetMapping("resumesLog")
    public RequestResult resumesLog(String studentId,Integer page,Integer pageSize){
        RequestResult result = new RequestResult();
        List<ResumeReceivedDto> list=infoEditService.getResumeReceivedByStudentId(studentId,page,pageSize);
        result.appendData("list",list);
        result.appendData("total",infoEditService.getResumeReceivedTotalByStudentId(studentId));
        return result;
    }

    @DeleteMapping("resumeReceived")
    public RequestResult resumeReceived(String id){
        RequestResult result = new RequestResult();
        infoEditService.delResumeReceived(id);
        return result;
    }

}
